package com.example.sa.myapp;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;

import com.example.sa.myapp.classify.Classify_Fragment;
import com.example.sa.myapp.home.Home_Fragment;
import com.example.sa.myapp.my_shopp.MyShopp_Fragment;
import com.example.sa.myapp.shopping_cart.ShoppCar_Fragment;


public class Home_Activity extends AppCompatActivity implements View.OnClickListener {

    //屏幕滑动监听器器
    GestureDetector mDetector;
    //判断fragment的数字
    private int laynumber = 1;

    //4个按钮
    private ImageButton imgBtn_home;//首页
    private ImageButton imgBtn_classify;//分类
    private ImageButton imgBtn_shoppCar;//购物车
    private ImageButton imgBtn_myshopp;//我的

    //4个fragment
    private Home_Fragment homeFragment = new Home_Fragment();//首页fragment
    private Classify_Fragment classifyFragment = new Classify_Fragment();//分类
    private ShoppCar_Fragment shoppCarFragment = new ShoppCar_Fragment();//购物车
    private MyShopp_Fragment myShoppFragment = new MyShopp_Fragment();//我的

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        //顶部菜单栏屏蔽
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }

        //4个按钮注册完工
        imgBtn_home = (ImageButton) findViewById(R.id.imgBtn_home);
        imgBtn_home.setOnClickListener(this);
        imgBtn_classify = (ImageButton) findViewById(R.id.imgBtn_classify);
        imgBtn_classify.setOnClickListener(this);
        imgBtn_shoppCar = (ImageButton) findViewById(R.id.imgBtn_shoppcar);
        imgBtn_shoppCar.setOnClickListener(this);
        imgBtn_myshopp = (ImageButton) findViewById(R.id.imgBtn_myshopp);
        imgBtn_myshopp.setOnClickListener(this);

        //调用屏幕监听器
        addAction();

        //显示首页
        replaceFragment(homeFragment);

    }

    @Override
    public void onClick(View v) {
        setTabDefaultColor();
        switch (v.getId()) {
            case R.id.imgBtn_home:
                imgBtn_home.setImageResource(R.drawable.tab_home_pressed);
                replaceFragment(homeFragment);
                break;
            case R.id.imgBtn_classify:
                imgBtn_classify.setImageResource(R.drawable.fx_tab_fenlei_pressed);
                replaceFragment(classifyFragment);
                break;
            case R.id.imgBtn_shoppcar:
                imgBtn_shoppCar.setImageResource(R.drawable.tab_shopping_pressed);
                replaceFragment(shoppCarFragment);
                break;
            case R.id.imgBtn_myshopp:
                imgBtn_myshopp.setImageResource(R.drawable.fx_tab_my_pressed);
                replaceFragment(myShoppFragment);
                break;
        }
    }

    //恢复底部菜单图片
    private void setTabDefaultColor() {
        imgBtn_home.setImageResource(R.drawable.tab_home_normal);
        imgBtn_classify.setImageResource(R.drawable.fx_tab_fenlei_normal);
        imgBtn_shoppCar.setImageResource(R.drawable.tab_shopping_normal);
        imgBtn_myshopp.setImageResource(R.drawable.fx_tab_my_normal);
    }

    //更换中间的flay_menu
    private void replaceFragment(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.flay_menu, fragment);
        transaction.commit();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return mDetector.onTouchEvent(event);
    }

    //屏幕监听器
    protected void addAction() {

        Log.d("MYTAG", "滑动开始");
        final float FLIP_DISTANCE = 10;

        mDetector = new GestureDetector(this, new GestureDetector.OnGestureListener() {

            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public void onShowPress(MotionEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public void onLongPress(MotionEvent e) {
                // TODO Auto-generated method stub

            }

            /**
             *
             * e1 The first down motion event that started the fling. e2 The
             * move motion event that triggered the current onFling.
             */
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

                float x = e2.getX() - e1.getX();
                float y = e2.getY() - e1.getY();
                if(Math.abs(x) > Math.abs(y))  {
                    if(x > FLIP_DISTANCE) {
                        Log.d("MYTAG", "向右滑...");
                        Right();
                        return true;
                    } else if( x < -FLIP_DISTANCE){
                        Log.d("MYTAG", "向左滑...");
                        Left();
                        return true;
                    }
                } else {
                    if(y > FLIP_DISTANCE) {
                        Log.d("MYTAG", "向下滑...");
                        Down();
                        return true;
                    } else if(y < -FLIP_DISTANCE){
                        Log.d("MYTAG", "向上滑...");
                        Up();
                        return true;
                    }
                }
                Log.d("TAG", e2.getX() + " " + e2.getY());
                return false;
            }

            @Override
            public boolean onDown(MotionEvent e) {
                // TODO Auto-generated method stub
                return false;
            }
        });
    }

    public void Left()
    {
        if(laynumber < 4) {
            laynumber++;
            Slide_view();
        }
    }

    public void Right()
    {
        if(laynumber > 1) {
            laynumber--;
            Slide_view();
        }
    }

    public void Up()
    {

    }

    public void Down()
    {

    }

    public void Slide_view() {
        setTabDefaultColor();
        switch (laynumber) {
            case 1:
                imgBtn_home.setImageResource(R.drawable.tab_home_pressed);
                replaceFragment(homeFragment);
                break;
            case 2:
                imgBtn_classify.setImageResource(R.drawable.fx_tab_fenlei_pressed);
                replaceFragment(classifyFragment);
                break;
            case 3:
                imgBtn_shoppCar.setImageResource(R.drawable.tab_shopping_pressed);
                replaceFragment(shoppCarFragment);
                break;
            case 4:
                imgBtn_myshopp.setImageResource(R.drawable.fx_tab_my_pressed);
                replaceFragment(myShoppFragment);
                break;
        }
    }
}
